package com.example.sps.controller;

import com.example.sps.commons.Constants;
import com.example.sps.commons.R;
import com.example.sps.pojo.dto.*;
import com.example.sps.pojo.vo.EmpToken;
import com.example.sps.pojo.vo.PageVO;
import com.example.sps.service.ProductService;
import com.example.sps.utils.AliossUtils;
import com.example.sps.utils.JWTUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @ProjectName: Sps
 * @Titile: ProductController
 * @Author: nency
 * @Description: TODO
 */
@RestController
@Slf4j
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private AliossUtils aliossUtils;

    @GetMapping("/parameter/list")
    public R<PageVO> getParameterList(CommonPageQueryDTO commonPageQueryDTO){
        log.info("参数分页查询,{}", commonPageQueryDTO);
        PageVO pageVO = productService.parameterPage(commonPageQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PutMapping("/parameter/update")
    public R<Void> updateParameter(@RequestBody ParameterUpdateOrAddDTO parameterUpdateOrAddDTO,
                                   HttpServletRequest request){
        log.info("修改参数,{}",parameterUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateParameter(parameterUpdateOrAddDTO,empToken);
        return R.ok("修改成功");
    }

    @PostMapping("/parameter/add")
    public R<Void> addParameter(@RequestBody ParameterUpdateOrAddDTO parameterUpdateOrAddDTO,
                                HttpServletRequest request){
        log.info("新增参数,{}",parameterUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addParameter(parameterUpdateOrAddDTO,empToken);
        return R.ok("新增成功");
    }

    @DeleteMapping("/parameter/delete/{id}")
    public R<Void> deleteParameter(@PathVariable Integer id){
        log.info("删除参数,该id为{}",id);
        productService.deleteParam(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/parameter/delete")
    public R<Void> deleteParamBatch(@RequestBody List<Integer> ids){
        log.info("批量删除参数,该id为{}",ids);
        productService.deleteParamBatch(ids);
        return R.ok("删除成功！");
    }

    @PutMapping("parameter/status")
    public R<Void> updateParamStatus(@RequestBody ParameterStatusDTO parameterStatusDTO,
                                HttpServletRequest request){
        log.info("修改参数状态,{}",parameterStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateParamStatus(parameterStatusDTO,empToken);
        return R.ok("修改状态成功");
    }

    @GetMapping("/smclass/list")
    public R<PageVO> getListSmClass(CommonPageQueryDTO commonPageQueryDTO){
        log.info("上门体检分类分页查询,{}", commonPageQueryDTO);
        PageVO pageVO = productService.smClassPage(commonPageQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PostMapping("/smclass/add")
    public R<Void> addSmClass(@RequestBody SmClassUpdateOrAddDTO smClassUpdateOrAddDTO,HttpServletRequest request){
        log.info("新增上门体检分类,{}",smClassUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addSmClass(smClassUpdateOrAddDTO,empToken);
        return R.ok("新增成功");
    }

    @PutMapping("/smclass/update")
    public R<Void> updateSmClass(@RequestBody SmClassUpdateOrAddDTO smClassUpdateOrAddDTO,HttpServletRequest request){
        log.info("修改上门体检分类,{}",smClassUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateSmClass(smClassUpdateOrAddDTO,empToken);
        return R.ok("修改成功");
    }

    @PutMapping("/smclass/status")
    public R<Void> updateStatus(@RequestBody SmClassStatusDTO smClassStatusDTO,
                                HttpServletRequest request){
        log.info("修改参数状态,{}",smClassStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateSmClassStatus(smClassStatusDTO,empToken);
        return R.ok("修改状态成功");
    }

    @DeleteMapping("/smclass/delete/{id}")
    public R<Void> deleteSmClass(@PathVariable Integer id){
        log.info("删除上门体检分类,该id为{}",id);
        productService.deleteSmClass(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/smclass/delete")
    public R<Void> deleteSmClassBatch(@RequestBody List<Integer> ids){
        log.info("批量删除上门体检分类,该id为{}",ids);
        productService.deleteSmclassBatch(ids);
        return R.ok("删除成功！");
    }

    @PostMapping("/smproduct/add")
    public R<Void> addSmProduct(@RequestBody SmProductDTO smProductDTO,
                                HttpServletRequest request){
        log.info("新增上门体检商品,{}",smProductDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addSmProduct(smProductDTO,empToken);
        return R.ok("新增成功");
    }


    @GetMapping("/smproduct/list")
    public R<PageVO> pageSmProduct(SmProductQueryDTO smProductQueryDTO){
        log.info("上门体检分类分页查询,{}",smProductQueryDTO);
        PageVO pageVO = productService.smProductPage(smProductQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PutMapping("/smproduct/update")
    public R<Void> updateSmProduct(@RequestBody SmProductDTO smProductDTO,
                                   HttpServletRequest request){
        log.info("修改上门体检商品,{}",smProductDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateSmProduct(smProductDTO,empToken);
        return R.ok("修改成功！");
    }

    @PutMapping("/smproduct/status")
    public R<Void> updateSmProductStatus(SmProductStatusDTO smProductStatusDTO,
                                         HttpServletRequest request){
        log.info("修改上门体检商品状态,{}",smProductStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateSmProductStatus(smProductStatusDTO,empToken);
        return R.ok("修改成功");
    }

    @DeleteMapping("/smproduct/delete/{id}")
    public R<Void> deleteSmProduct(@PathVariable Long id){
        log.info("删除上门体检商品,该id为{}",id);
        productService.deleteSmProduct(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/smproduct/delete")
    public R<Void> deleteSmProductBatch(@RequestBody List<Long> ids){
        log.info("批量删除上门体检商品,该id为{}",ids);
        productService.deleteSmProductBatch(ids);
        return R.ok("删除成功！");
    }

    @GetMapping("/homeclass/list")
    public R<PageVO> getListHomeClass(CommonPageQueryDTO commonPageQueryDTO){
        log.info("家政护理分类分页查询,{}", commonPageQueryDTO);
        PageVO pageVO = productService.homeClassPage(commonPageQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PostMapping("/homeclass/add")
    public R<Void> addSmClass(@RequestBody HomeClassUpdateOrAddDTO homeClassUpdateOrAddDTO,HttpServletRequest request){
        log.info("新增家政护理分类,{}",homeClassUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addHomeClass(homeClassUpdateOrAddDTO,empToken);
        return R.ok("新增成功");
    }

    @PutMapping("/homeclass/update")
    public R<Void> updateSmClass(@RequestBody HomeClassUpdateOrAddDTO homeClassUpdateOrAddDTO,HttpServletRequest request){
        log.info("修改家政护理分类,{}",homeClassUpdateOrAddDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateHomeClass(homeClassUpdateOrAddDTO,empToken);
        return R.ok("修改成功");
    }

    @PutMapping("/homeclass/status")
    public R<Void> updateStatus(@RequestBody HomeClassStatusDTO homeClassStatusDTO,
                                HttpServletRequest request){
        log.info("修改家政护理状态,{}",homeClassStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateHomeClassStatus(homeClassStatusDTO,empToken);
        return R.ok("修改状态成功");
    }

    @DeleteMapping("/homeclass/delete/{id}")
    public R<Void> deleteHomeClass(@PathVariable Integer id){
        log.info("删除家政护理分类,该id为{}",id);
        productService.deleteHomeClass(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/homeclass/delete")
    public R<Void> deleteHomeClassBatch(@RequestBody List<Integer> ids){
        log.info("批量删除家政护理分类,该id为{}",ids);
        productService.deleteHomeClassBatch(ids);
        return R.ok("删除成功！");
    }

    @PostMapping("/homeproduct/add")
    public R<Void> addHomeProduct(@RequestBody HomeProductDTO homeProductDTO,
                                HttpServletRequest request){
        log.info("新增家政护理商品,{}",homeProductDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addHomeProduct(homeProductDTO,empToken);
        return R.ok("新增成功");
    }

    @GetMapping("/homeproduct/list")
    public R<PageVO> pageHomeProduct(HomeProductQueryDTO homeProductQueryDTO){
        log.info("家政护理分类分页查询,{}",homeProductQueryDTO);
        PageVO pageVO = productService.homeProductPage(homeProductQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PutMapping("/homeproduct/update")
    public R<Void> updateSmProduct(@RequestBody HomeProductDTO homeProductDTO,
                                   HttpServletRequest request){
        log.info("修改家政护理商品,{}",homeProductDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateHomeProduct(homeProductDTO,empToken);
        return R.ok("修改成功！");
    }

    @PutMapping("/homeproduct/status")
    public R<Void> updateHomeProductStatus(HomeProductStatusDTO homeProductStatusDTO,
                                         HttpServletRequest request){
        log.info("修改家政护理商品状态,{}",homeProductStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateHomeProductStatus(homeProductStatusDTO,empToken);
        return R.ok("修改成功");
    }

    @DeleteMapping("/homeproduct/delete/{id}")
    public R<Void> deleteHomeProduct(@PathVariable Long id){
        log.info("删除家政护理商品,该id为{}",id);
        productService.deleteHomeProduct(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/homeproduct/delete")
    public R<Void> deleteHomeProductBatch(@RequestBody List<Long> ids){
        log.info("批量删除家政护理商品,该id为{}",ids);
        productService.deleteHomeProductBatch(ids);
        return R.ok("删除成功！");
    }

    @PostMapping("/project/add")
    public R<Void> addProject(@RequestBody ProjectDTO projectDTO,HttpServletRequest request){
        log.info("新增服务项目,{}",projectDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.addProject(projectDTO,empToken);
        return R.ok("新增成功");
    }

    @GetMapping("/project/list")
    public R<PageVO> pageHomeProduct(ProjectQueryDTO projectQueryDTO){
        log.info("服务项目分页查询,{}",projectQueryDTO);
        PageVO pageVO = productService.ProjectPage(projectQueryDTO);
        return R.ok("查询成功",pageVO);
    }

    @PutMapping("/project/update")
    public R<Void> updateSmProduct(@RequestBody ProjectDTO projectDTO,
                                   HttpServletRequest request){
        log.info("修改服务项目,{}",projectDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateProject(projectDTO,empToken);
        return R.ok("修改成功！");
    }

    @PutMapping("/project/status")
    public R<Void> updateStatus(@RequestBody ProjectStatusDTO projectStatusDTO,
                                HttpServletRequest request){
        log.info("修改服务项目状态,{}",projectStatusDTO);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        EmpToken empToken = JWTUtils.parseToken(token);
        productService.updateProjectStatus(projectStatusDTO,empToken);
        return R.ok("修改状态成功");
    }

    @DeleteMapping("/project/delete/{id}")
    public R<Void> deleteProject(@PathVariable Integer id){
        log.info("删除服务项目,该id为{}",id);
        productService.deleteProject(id);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/project/delete")
    public R<Void> deleteProjectBatch(@RequestBody List<Integer> ids){
        log.info("批量删除服务项目,该id为{}",ids);
        productService.deleteProjectBatch(ids);
        return R.ok("删除成功！");
    }

}
